package com.itheima.search.listenner;


import com.itheima.search.constant.MqConstants;
import com.itheima.search.domain.News;
import com.itheima.search.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SearchListenner {

    @Autowired
    private SearchService searchService;


    /**
     * 监听新闻添加到es的消息
     * @param news
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.NEWS_GET_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstants.NEWS_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = {MqConstants.NEWS_ADD_ROUTING_KEY}
    ))
    private void addNews(News news) {
        searchService.AddDocument(news);
    }

    /**
     * 监听新闻从es删除的消息
     * @param newsId
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.NEWS_DEL_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstants.NEWS_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = {MqConstants.NEWS_DEL_ROUTING_KEY}
    ))
    private void delNews(String newsId) {
        searchService.delDocument(newsId);
    }

    /**
     * 监听新闻从es更新的消息（先删后改）
     * @param news
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.NEWS_UDP_QUEUE, durable = "true"),
            exchange = @Exchange(name = MqConstants.NEWS_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = {MqConstants.NEWS_UDP_ROUTING_KEY}
    ))
    private void udpNews(News news) {
        Long newsId = news.getNewsId();
        // 先将数据删除
        searchService.delDocument(newsId.toString());
        // 再将数据添加到es中
        searchService.AddDocument(news);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delete.news.queue", durable = "true"),
            exchange = @Exchange(name = "news-exchange", type = "topic"),
            key = {"news.delete"}
    ))
    private void deleteNews(List<String> newIds) {
        for (String newId : newIds) {
            // 删除索引
            searchService.deleteNews(newId);
        }
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "update.news.queue", durable = "true"),
            exchange = @Exchange(name = "news-exchange", type = "topic"),
            key = {"news.update"}
    ))
    private void updateNews(String newIds) {
        //根据id查询数据的数据并更新es数据
        searchService.updateNewsById(newIds);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "save.news.queue", durable = "true"),
            exchange = @Exchange(name = "news-exchange", type = "topic"),
            key = {"news.save"}
    ))
    private void saveNews(List<String> newsIds) {
        //根据id查询数据的数据并更新es数据
        newsIds.stream().forEach(newIds->{
            searchService.addNewsToEs(newIds);
        });
    }
}
